home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000015.txt < prev    next >
Text File  |  2013-04-03  |  6KB  |  163 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // Routines used to validate and normalize arguments.
  6.  
  7. var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
  8.  
  9. // TODO(benwells): unit test this file.
  10. // JSONSchemaValidator is not loaded in unit tests.
  11. var validate;
  12. if (chromeHidden.JSONSchemaValidator) {
  13.   var schemaValidator = new chromeHidden.JSONSchemaValidator();
  14.  
  15.   // Validate arguments.
  16.   validate = function(args, parameterSchemas) {
  17.     if (args.length > parameterSchemas.length)
  18.       throw new Error("Too many arguments.");
  19.     for (var i = 0; i < parameterSchemas.length; i++) {
  20.       if (i in args && args[i] !== null && args[i] !== undefined) {
  21.         schemaValidator.resetErrors();
  22.         schemaValidator.validate(args[i], parameterSchemas[i]);
  23.         if (schemaValidator.errors.length == 0)
  24.           continue;
  25.         var message = "Invalid value for argument " + (i + 1) + ". ";
  26.         for (var i = 0, err;
  27.             err = schemaValidator.errors[i]; i++) {
  28.           if (err.path) {
  29.             message += "Property '" + err.path + "': ";
  30.           }
  31.           message += err.message;
  32.           message = message.substring(0, message.length - 1);
  33.           message += ", ";
  34.         }
  35.         message = message.substring(0, message.length - 2);
  36.         message += ".";
  37.         throw new Error(message);
  38.       } else if (!parameterSchemas[i].optional) {
  39.         throw new Error("Parameter " + (i + 1) + " (" +
  40.             parameterSchemas[i].name + ") is required.");
  41.       }
  42.     }
  43.   };
  44. } else {
  45.   validate = function() {};
  46. }
  47.  
  48. // Generate all possible signatures for a given API function.
  49. function getSignatures(parameterSchemas) {
  50.   if (parameterSchemas.length === 0)
  51.     return [[]];
  52.   var signatures = [];
  53.   var remaining = getSignatures(parameterSchemas.slice(1));
  54.   for (var i = 0; i < remaining.length; i++)
  55.     signatures.push([parameterSchemas[0]].concat(remaining[i]))
  56.   if (parameterSchemas[0].optional)
  57.     return signatures.concat(remaining);
  58.   return signatures;
  59. };
  60.  
  61. // Return true if arguments match a given signature's schema.
  62. function argumentsMatchSignature(args, candidateSignature) {
  63.   if (args.length != candidateSignature.length)
  64.     return false;
  65.   for (var i = 0; i < candidateSignature.length; i++) {
  66.     var argType =  chromeHidden.JSONSchemaValidator.getType(args[i]);
  67.     if (!schemaValidator.isValidSchemaType(argType,
  68.         candidateSignature[i]))
  69.       return false;
  70.   }
  71.   return true;
  72. };
  73.  
  74. // Finds the function signature for the given arguments.
  75. function resolveSignature(args, definedSignature) {
  76.   var candidateSignatures = getSignatures(definedSignature);
  77.   for (var i = 0; i < candidateSignatures.length; i++) {
  78.     if (argumentsMatchSignature(args, candidateSignatures[i]))
  79.       return candidateSignatures[i];
  80.   }
  81.   return null;
  82. };
  83.  
  84. // Returns a string representing the defined signature of the API function.
  85. // Example return value for chrome.windows.getCurrent:
  86. // "windows.getCurrent(optional object populate, function callback)"
  87. function getParameterSignatureString(name, definedSignature) {
  88.   var getSchemaTypeString = function(schema) {
  89.     var schemaTypes = schemaValidator.getAllTypesForSchema(schema);
  90.     var typeName = schemaTypes.join(" or ") + " " + schema.name;
  91.     if (schema.optional)
  92.       return "optional " + typeName;
  93.     return typeName;
  94.   };
  95.   var typeNames = definedSignature.map(getSchemaTypeString);
  96.   return name + "(" + typeNames.join(", ") + ")";
  97. };
  98.  
  99. // Returns a string representing a call to an API function.
  100. // Example return value for call: chrome.windows.get(1, callback) is:
  101. // "windows.get(int, function)"
  102. function getArgumentSignatureString(name, args) {
  103.   var typeNames = args.map(chromeHidden.JSONSchemaValidator.getType);
  104.   return name + "(" + typeNames.join(", ") + ")";
  105. };
  106.  
  107. // Finds the correct signature for the given arguments, then validates the
  108. // arguments against that signature. Returns a 'normalized' arguments list
  109. // where nulls are inserted where optional parameters were omitted.
  110. // |args| is expected to be an array.
  111. function normalizeArgumentsAndValidate(args, funDef) {
  112.   if (funDef.allowAmbiguousOptionalArguments) {
  113.     validate(args, funDef.definition.parameters);
  114.     return args;
  115.   }
  116.   var definedSignature = funDef.definition.parameters;
  117.   var resolvedSignature = resolveSignature(args, definedSignature);
  118.   if (!resolvedSignature)
  119.     throw new Error("Invocation of form " +
  120.         getArgumentSignatureString(funDef.name, args) +
  121.         " doesn't match definition " +
  122.         getParameterSignatureString(funDef.name, definedSignature));
  123.   validate(args, resolvedSignature);
  124.   var normalizedArgs = [];
  125.   var ai = 0;
  126.   for (var si = 0; si < definedSignature.length; si++) {
  127.     if (definedSignature[si] === resolvedSignature[ai])
  128.       normalizedArgs.push(args[ai++]);
  129.     else
  130.       normalizedArgs.push(null);
  131.   }
  132.   return normalizedArgs;
  133. };
  134.  
  135. // Validates that a given schema for an API function is not ambiguous.
  136. function isFunctionSignatureAmbiguous(functionDef) {
  137.   if (functionDef.allowAmbiguousOptionalArguments)
  138.     return false;
  139.   var signaturesAmbiguous = function(signature1, signature2) {
  140.     if (signature1.length != signature2.length)
  141.       return false;
  142.     for (var i = 0; i < signature1.length; i++) {
  143.       if (!schemaValidator.checkSchemaOverlap(
  144.           signature1[i], signature2[i]))
  145.         return false;
  146.     }
  147.     return true;
  148.   };
  149.   var candidateSignatures = getSignatures(functionDef.parameters);
  150.   for (var i = 0; i < candidateSignatures.length; i++) {
  151.     for (var j = i + 1; j < candidateSignatures.length; j++) {
  152.       if (signaturesAmbiguous(candidateSignatures[i], candidateSignatures[j]))
  153.         return true;
  154.     }
  155.   }
  156.   return false;
  157. };
  158.  
  159. exports.isFunctionSignatureAmbiguous = isFunctionSignatureAmbiguous;
  160. exports.normalizeArgumentsAndValidate = normalizeArgumentsAndValidate;
  161. exports.schemaValidator = schemaValidator;
  162. exports.validate = validate;
  163.